import { resExample } from './common/responseData'
import { IResponse } from '@src/libcommon'
import { sendCode } from './common/sendCode'
import { IBody } from '@src/routes/common/types'

import HttpStatusCodes from '@src/common/HttpStatusCodes'

class PublicService {
  /**
   * 发送验证码
   * @param {IBody} body
   * @returns {Promise<resExample>}
   */
  code = async (body: IBody): Promise<IResponse> => {
    try {
      const { email, type = "register" } = body
      await sendCode(email as string, type as string)

      // TODO: 发送验证码
      return resExample({
        code: HttpStatusCodes.OK,
        message: "验证码发送成功",
        success: true,
        data: null,
      })
    }
    catch (err) {
      throw resExample({
        code: HttpStatusCodes.INTERNAL_SERVER_ERROR,
        message: "验证码发送失败",
        success: false,
        data: null,
      })
    }
  }
  /**
   * 上传图片
   * @param {File} file
   * @returns {Promise<resExample>}
   */
  upload = async (file: Express.Multer.File): Promise<IResponse> => {
    console.log('upload file:', file);
    return resExample({
      code: HttpStatusCodes.OK,
      message: "上传成功",
      success: true,
      data: file.filename
    })
  }
}

export default new PublicService();